package boundary;

import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.filechooser.FileFilter;
import store.TutteRicette;
import store.TuttiCibi;
import store.TuttiMenu;
import exceptions.DBException;
import boundary.Home.stati;
import boundary.Ricerca.Tipo;
import java.awt.Insets;
import java.io.File;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import calculator.EsportaImporta;
import java.awt.event.KeyEvent;
import javax.swing.JCheckBox;
import javax.swing.SwingConstants;
import javax.swing.BorderFactory;
import javax.swing.border.BevelBorder;

public class Esporta extends JPanel {
	
	private static final long serialVersionUID = 1L;
	private JLabel titolo = null;
	private Ricerca ricercaCibi = null;
	private JButton esportaB = null;
	private Home home;
	private GridBagConstraints gbcCentro;
	
	private enum Stato {
		INIT, CIBI, RICETTE, MENU
	};
	
	private Stato stato;
	private JPanel initP = null;
	private JLabel jLabel = null;
	private JPanel buttonBar = null;
	private JButton indietroB = null;
	private JButton avantiB = null;
	private Ricerca ricercaRicette = null;
	private Ricerca ricercaMenu = null;
	private JLabel jLabel1 = null;
	private JCheckBox cibiCB = null;
	private JCheckBox ricetteCB = null;
	private JCheckBox menuCB = null;
	private JPanel abilitazioni = null;
	
	/**
	 * This is the default constructor
	 */
	public Esporta(Home home) {
		super();
		this.home = home;
		initialize();
		setStato(Stato.INIT);
	}
	
	public void setStato(Stato s) {
		stato = s;
		titolo.setText("Ricerca " + s.toString().toLowerCase());
		esportaB.setEnabled(true);
		avantiB.setEnabled(true);
		indietroB.setEnabled(true);
		switch (s) {
			case INIT:
				titolo.setText("Esportazione dati");
				esportaB.setEnabled(false);
				indietroB.setEnabled(false);
				break;
			case MENU:
				avantiB.setEnabled(false);
		}
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		GridBagConstraints gbcButtonBar = new GridBagConstraints();
		gbcButtonBar.gridx = 0;
		gbcButtonBar.gridy = 2;
		gbcButtonBar.insets = new Insets(5, 5, 5, 5);
		gbcButtonBar.anchor = GridBagConstraints.WEST;
		gbcButtonBar.fill = GridBagConstraints.HORIZONTAL;
		jLabel = new JLabel();
		jLabel.setText("Per esportare i dati selezionare i criteri di ricerca per i cibi, le ricette e i menu");
		jLabel.setFont(new Font("Dialog", Font.BOLD, 12));
		gbcCentro = new GridBagConstraints();
		gbcCentro.gridx = 0;
		gbcCentro.insets = new Insets(5, 5, 5, 5);
		gbcCentro.gridy = 1;
		GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
		gridBagConstraints11.fill = GridBagConstraints.BOTH;
		gridBagConstraints11.gridy = 1;
		gridBagConstraints11.weightx = 1.0;
		gridBagConstraints11.weighty = 1.0;
		gridBagConstraints11.insets = new Insets(3, 3, 3, 3);
		gridBagConstraints11.gridx = 0;
		GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
		gridBagConstraints1.fill = GridBagConstraints.BOTH;
		gridBagConstraints1.gridy = 1;
		gridBagConstraints1.weightx = 1.0;
		gridBagConstraints1.weighty = 1.0;
		gridBagConstraints1.gridx = 0;
		GridBagConstraints gbcTitolo = new GridBagConstraints();
		gbcTitolo.gridx = 0;
		gbcTitolo.insets = new Insets(5, 5, 5, 5);
		gbcTitolo.anchor = GridBagConstraints.CENTER;
		gbcTitolo.gridy = 0;
		titolo = new JLabel();
		titolo.setText("Seleziona criteri di ricerca");
		titolo.setFont(new Font("Dialog", Font.BOLD, 20));
		this.setSize(474, 241);
		this.setLayout(new GridBagLayout());
		this.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		this.add(titolo, gbcTitolo);
		this.add(getButtonBar(), gbcButtonBar);
		this.add(getInitP(), gbcCentro);
		getRicercaCibi();
		getRicercaMenu();
		getRicercaRicette();
	}
	
	/**
	 * This method initializes ricercaCibi
	 * 
	 * @return javax.swing.JPanel
	 */
	public Ricerca getRicercaCibi() {
		if (ricercaCibi == null) {
			ricercaCibi = new Ricerca(Tipo.CIBI, home);
		}
		return ricercaCibi;
	}
	
	/**
	 * This method initializes ricercaB
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton geEsportaB() {
		if (esportaB == null) {
			esportaB = new JButton();
			esportaB.setText("Esporta");
			esportaB.setMnemonic(KeyEvent.VK_UNDEFINED);
			esportaB.setIcon(new ImageIcon(getClass().getResource("/icone/page-up-icon.png")));
			esportaB.setFont(new Font("Dialog", Font.BOLD, 12));
			esportaB.setCursor(new Cursor(Cursor.HAND_CURSOR));
			esportaB.setPreferredSize(new Dimension(111, 30));
			esportaB.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (!checkCriteri()) return;
					setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
					
					JFileChooser fc = new JFileChooser();
					fc.setAcceptAllFileFilterUsed(false);
					fc.setCurrentDirectory(new File(EsportaImporta.EXPORT_DIR));
					fc.setSelectedFile(new File(EsportaImporta.EXPORT_DIR + "export.ecd"));
					fc.addChoosableFileFilter(new FileFilter() {
						@Override
						public String getDescription() {
							return "EasyCooking File (*.ecd)";
						}
						
						@Override
						public boolean accept(File f) {
							return (f.isDirectory() || f.getAbsolutePath().endsWith(".ecd"));
						}
					});
					if (fc.showSaveDialog(home) == JFileChooser.APPROVE_OPTION) {
						try {
							home.gie.esporta(ricercaCibi.getCriterioRicerca(), ricercaRicette.getCriterioRicerca(), ricercaMenu.getCriterioRicerca(), fc.getSelectedFile().getPath());
							JOptionPane.showMessageDialog(home, "Esportazione dei dati riuscita", "Operazione conclusa", JOptionPane.INFORMATION_MESSAGE);
							home.setStato(stati.ESPORTAZIONE);
						} catch (Exception e1) {
							JOptionPane.showMessageDialog(home, "Esportazione dei dati fallita", "Operazione Fallita", JOptionPane.ERROR_MESSAGE);
							setCursor(Cursor.getDefaultCursor());
						} 
					}
					setCursor(Cursor.getDefaultCursor());
				}
			});
		}
		return esportaB;
	}
	
	private boolean checkCriteri() {
		boolean noC, noR, noM;
		try {
			noC = TuttiCibi.getTuttiCibi().ricerca(ricercaCibi.getCriterioRicerca()).size() == 0;
			noR = TutteRicette.getTutteRicette().ricercare(ricercaRicette.getCriterioRicerca()).size() == 0;
			noM = TuttiMenu.getTuttiMenu().ricercare(ricercaMenu.getCriterioRicerca()).size() == 0;
			if (noC || noR || noM) {
				String message = "Con i criteri di ricerca impostati non viene prodotto nessun risultato per ";
				if (noC) message += "cibi, ";
				if (noR) message += "ricette, ";
				if (noM) message += "menu, ";
				message = message.substring(0, message.lastIndexOf(','));
				setCursor(Cursor.getDefaultCursor());
				return JOptionPane.showOptionDialog(home, message + ". Procedere lo stesso?", "Attenzione", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null) == JOptionPane.YES_OPTION;
			}
			return true;
		} catch (DBException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * This method initializes initP
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getInitP() {
		if (initP == null) {
			GridBagConstraints gridBagConstraints61 = new GridBagConstraints();
			gridBagConstraints61.gridx = 1;
			gridBagConstraints61.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints61.anchor = GridBagConstraints.CENTER;
			gridBagConstraints61.gridy = 1;
			jLabel1 = new JLabel();
			jLabel1.setText("Abilita ricerca per: ");
			jLabel1.setFont(new Font("Dialog", Font.PLAIN, 12));
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.gridy = 0;
			gridBagConstraints3.gridwidth = 2;
			gridBagConstraints3.insets = new Insets(5, 5, 5, 5);
			initP = new JPanel();
			initP.setLayout(new GridBagLayout());
			initP.add(jLabel, gridBagConstraints3);
			initP.add(getAbilitazioni(), gridBagConstraints61);
		}
		return initP;
	}
	
	/**
	 * This method initializes buttonBar
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getButtonBar() {
		if (buttonBar == null) {
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = 2;
			gridBagConstraints6.anchor = GridBagConstraints.EAST;
			gridBagConstraints6.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints6.gridy = 0;
			gridBagConstraints6.fill = GridBagConstraints.NONE;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 1;
			gridBagConstraints4.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints4.anchor = GridBagConstraints.CENTER;
			gridBagConstraints4.fill = GridBagConstraints.BOTH;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.fill = GridBagConstraints.BOTH;
			gridBagConstraints2.anchor = GridBagConstraints.WEST;
			gridBagConstraints2.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints2.gridy = 0;
			buttonBar = new JPanel();
			buttonBar.setLayout(new GridBagLayout());
			buttonBar.setMinimumSize(new Dimension(340, 30));
			buttonBar.setPreferredSize(new Dimension(340, 30));
			buttonBar.add(geEsportaB(), gridBagConstraints4);
			buttonBar.add(getIndietroB(), gridBagConstraints2);
			buttonBar.add(getAvantiB(), gridBagConstraints6);
		}
		return buttonBar;
	}
	
	/**
	 * This method initializes indietroB
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getIndietroB() {
		if (indietroB == null) {
			indietroB = new JButton();
			indietroB.setText("Indietro");
			indietroB.setFont(new Font("Dialog", Font.BOLD, 12));
			indietroB.setCursor(new Cursor(Cursor.HAND_CURSOR));
			indietroB.setIcon(new ImageIcon(getClass().getResource("/icone/back-icon.png")));
			indietroB.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					cambiaPannello(false);
				}
			});
		}
		return indietroB;
	}
	
	/**
	 * This method initializes avantiB
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getAvantiB() {
		if (avantiB == null) {
			avantiB = new JButton();
			avantiB.setText("Avanti");
			avantiB.setPreferredSize(new Dimension(101, 30));
			avantiB.setFont(new Font("Dialog", Font.BOLD, 12));
			avantiB.setCursor(new Cursor(Cursor.HAND_CURSOR));
			avantiB.setHorizontalTextPosition(SwingConstants.LEADING);
			avantiB.setIcon(new ImageIcon(getClass().getResource("/icone/next-icon.png")));
			avantiB.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					cambiaPannello(true);
				}
			});
		}
		return avantiB;
	}
	
	private void cambiaPannello(boolean avanti) {
		if (avanti) {
			switch (stato) {
				case INIT:
					mettiAlCentro(getRicercaCibi());
					setStato(Stato.CIBI);
					break;
				case CIBI:
					mettiAlCentro(getRicercaRicette());
					setStato(Stato.RICETTE);
					break;
				case RICETTE:
					mettiAlCentro(getRicercaMenu());
					setStato(Stato.MENU);
					break;
				default:
			}
		} else {
			switch (stato) {
				case RICETTE:
					mettiAlCentro(getRicercaCibi());
					setStato(Stato.CIBI);
					break;
				case MENU:
					mettiAlCentro(getRicercaRicette());
					setStato(Stato.RICETTE);
					break;
				case CIBI:
					mettiAlCentro(getInitP());
					setStato(Stato.INIT);
					break;
				default:
			}
		}
	}
	
	private void mettiAlCentro(JPanel p) {
		getComponent(2).setVisible(false);
		remove(2);
		add(p, gbcCentro);
		p.setVisible(true);
		p.requestFocus();
	}
	
	/**
	 * This method initializes ricercaRicette
	 * 
	 * @return boundary.Ricerca
	 */
	private Ricerca getRicercaRicette() {
		if (ricercaRicette == null) {
			ricercaRicette = new Ricerca(Tipo.RICETTE, home);
		}
		return ricercaRicette;
	}
	
	/**
	 * This method initializes ricercaMenu
	 * 
	 * @return boundary.Ricerca
	 */
	private Ricerca getRicercaMenu() {
		if (ricercaMenu == null) {
			ricercaMenu = new Ricerca(Tipo.MENU, home);
		}
		return ricercaMenu;
	}
	
	/**
	 * This method initializes cibiCB
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getCibiCB() {
		if (cibiCB == null) {
			cibiCB = new JCheckBox();
			cibiCB.setText("cibi");
			cibiCB.setFont(new Font("Dialog", Font.PLAIN, 12));
			cibiCB.setSelected(true);
			cibiCB.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					ricercaCibi.setEnabled(getCibiCB().isSelected());
				}
			});
		}
		return cibiCB;
	}
	
	/**
	 * This method initializes ricetteCB
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getRicetteCB() {
		if (ricetteCB == null) {
			ricetteCB = new JCheckBox();
			ricetteCB.setText("ricette");
			ricetteCB.setFont(new Font("Dialog", Font.PLAIN, 12));
			ricetteCB.setSelected(true);
			ricetteCB.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					ricercaRicette.setEnabled(getRicetteCB().isSelected());
				}
			});
		}
		return ricetteCB;
	}
	
	/**
	 * This method initializes menuCB
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getMenuCB() {
		if (menuCB == null) {
			menuCB = new JCheckBox();
			menuCB.setText("menu");
			menuCB.setFont(new Font("Dialog", Font.PLAIN, 12));
			menuCB.setSelected(true);
			menuCB.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					ricercaMenu.setEnabled(getMenuCB().isSelected());
				}
			});
		}
		return menuCB;
	}
	
	/**
	 * This method initializes abilitazioni
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getAbilitazioni() {
		if (abilitazioni == null) {
			GridBagConstraints gbcMenuCB = new GridBagConstraints();
			gbcMenuCB.gridx = 1;
			gbcMenuCB.anchor = GridBagConstraints.WEST;
			gbcMenuCB.insets = new Insets(5, 5, 5, 5);
			gbcMenuCB.gridy = 2;
			GridBagConstraints gbcRicetteCB = new GridBagConstraints();
			gbcRicetteCB.gridx = 1;
			gbcRicetteCB.anchor = GridBagConstraints.WEST;
			gbcRicetteCB.insets = new Insets(5, 5, 5, 5);
			gbcRicetteCB.gridy = 1;
			GridBagConstraints gbcCibiCB = new GridBagConstraints();
			gbcCibiCB.gridx = 1;
			gbcCibiCB.anchor = GridBagConstraints.WEST;
			gbcCibiCB.insets = new Insets(5, 5, 5, 5);
			gbcCibiCB.gridy = 0;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints.anchor = GridBagConstraints.WEST;
			gridBagConstraints.gridy = 0;
			abilitazioni = new JPanel();
			abilitazioni.setLayout(new GridBagLayout());
			abilitazioni.add(jLabel1, gridBagConstraints);
			abilitazioni.add(getCibiCB(), gbcCibiCB);
			abilitazioni.add(getRicetteCB(), gbcRicetteCB);
			abilitazioni.add(getMenuCB(), gbcMenuCB);
		}
		return abilitazioni;
	}
} //  @jve:decl-index=0:visual-constraint="0,0"
